﻿
function initCalendar(elementId) {
    $(document).ready(function() {
        $(document).ajaxStart(function() {
            $.modal(
                "<div>Loading.  Please Wait...</div>",
                {
                    containerId: 'modal-wait',
                    close: false,
                    escapeClose: false
                }
                );
        });

        $(document).ajaxStop(function() {
            $.modal.close();
        });

        $(elementId).fullCalendar({
            header: {
                left: 'prev,next,today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            buttonText: {
                prev: '&nbsp;&lt;&nbsp;',
                next: '&nbsp;&gt;&nbsp;'
            },
            //timeFormat: 'H{mm}t',
            weekMode: 'variable',
            events: queryCalendarItems,
            eventClick: onEventClicked
        });
    });
}

function queryCalendarItems(start, end, callback) {
    var proxy = new ServiceProxy(serviceUrl);
    proxy.isWcf = false;
    proxy.invoke("GetCalendarItems",
        { start: start, end: end },
        function(result) {
            //debugger
            var events = [];

            if (result) {
                jQuery.each(result, function(i, v) {
                    var cssClass = "color_" + v.Color;
                    
                    events.push({
                        title: v.Subject,
                        start: v.Start,
                        end: (!v.AllDay) ? v.End : null,
                        allDay: v.AllDay,
                        location: v.Location,
                        body: v.Body,
                        className: cssClass,
                        itemId: v.ItemId,
                        categories: v.Categories
                    });
                });
            }

            callback(events);
        },
        function(error, i, request) {
            alert(error);
        },
        false);
}

function getItemBody(item) {
    var proxy = new ServiceProxy(serviceUrl);
    proxy.isWcf = false;
    proxy.invoke("GetCalendarItemBody",
        { itemId: item.itemId },
        function(result) {
            //debugger
            var events = [];

            if (result) {
                item.body = result;
            }
            setTimeout(function() { displayItemDetails(item) }, 100);
        },
        function(error, i, request) {
            //alert(error);
            setTimeout(function() { displayItemDetails(item) }, 100);
        },
        false);
}

function onEventClicked(item, evt, view) {
    getItemBody(item);
}

function displayItemDetails(item) {
    // show a dialog with the item details
    $.modal(
        tmpl("excal_details", item),
        {
            containerId: 'main-box',
            close: true,
            escapeClose: true
        }
        );
}